<template>
  <div class="setting">
    <el-tabs>
      <el-tab-pane label="角色管理">
        <el-button
          type="primary"
          size="small"
          @click="addEvent"
        >新增角色</el-button>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号" width="80px" />
          <el-table-column label="角色" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                type="success"
                size="mini"
                @click="setEvent(row)"
              >分配权限</el-button>
              <el-button
                type="primary"
                size="mini"
                @click="editEvent(row)"
              >编辑</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="delEvent(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: right; padding-top: 20px">
          <el-pagination
            :current-page.sync="page.page"
            :page-sizes="[1, 5, 10, 100, 200, 300, 400]"
            :page-size.sync="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="getData"
            @current-change="getData"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane label="公司信息" lazy>
        <Info />
      </el-tab-pane>
    </el-tabs>
    <!-- 新增组件弹框 -->
    <Add ref="add" @getData="getData" />
    <!-- 分配权限弹框 -->
    <SetPermission ref="set" />
  </div>
</template>
<script>
import { sysRole, sysRoleDelete } from '@/api/setting'
import SetPermission from './components/setPermission'
import Add from './components/add.vue'
export default {
  components: {
    Add,
    Info: () => import('./components/info.vue'),
    SetPermission
  },
  data() {
    return {
      tableData: [{}],
      total: 50,
      page: {
        page: 1,
        pagesize: 5
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole(this.page)
      this.tableData = res.data.rows
      // 存储总条数
      this.total = res.data.total
      console.log('角色列表', res)
    },
    addEvent() {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    delEvent(id) {
      this.$confirm('您确定删除该项吗')
        .then(async() => {
          await sysRoleDelete(id)
          this.$message.success('删除成功')
          if (this.tableData.length === 1 && this.page.page > 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑点击
    editEvent(row) {
      // 打开弹框
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'edit'
      // 数据回显
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 分配权限点击
    setEvent(row) {
      this.$refs.set.isShow = true
      this.$refs.set.getData()
      this.$refs.set.getRoleInfo(row.id)
      this.$refs.set.title = row.name
    }
  }
}
</script>
<style lang="scss" scoped>
.setting {
  padding: 20px;
}
</style>
